package com.starbaba.invitate.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.invitate.model.InvitateFriendStat;
import com.starbaba.stat.utils.Constant;

@SuppressWarnings("rawtypes")
@Component
public class InvitateFriendStatService {

	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);  //正式stat
	private BaseDao daoLoan = new BaseDao("tt1");  ///数据库loan
	
	
	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String showway = (String) paramsMap.get("showway");
		String activity = (String) paramsMap.get("activity");
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startTime)  && !"".equals(startTime)) {
			whereParams.append("AND ic.cdate >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)  && !"".equals(endTime)) {
			whereParams.append("AND ic.cdate <= '" + endTime + "' ");
		}
		
		StringBuilder listSql = new StringBuilder("SELECT ic.cdate time,ic.invitateCount,ac.activityCount,info.newInfoCount FROM "
				+ "(SELECT invi.cdate, COUNT(*) invitateCount FROM loan_invitate invi GROUP BY invi.cdate) ic "
				+ "LEFT OUTER JOIN (SELECT COUNT(*) activityCount,u.activedate  FROM comm_user u WHERE u.id IN "
				+ "(SELECT tem.invitee FROM loan_invitate tem) GROUP BY u.activedate) ac ON ic.cdate = ac.activedate "
				+ "LEFT OUTER JOIN ("
				+ "SELECT DATE_FORMAT(info.createtime,'%Y-%m-%d') createtime ,COUNT(*) newInfoCount FROM loan_user_info info "
				+ "WHERE info.userid IN (SELECT tem.invitee FROM loan_invitate tem)"
				+ "GROUP BY DATE_FORMAT(info.createtime,'%Y-%m-%d')) info ON ic.cdate = info.createtime WHERE 1=1 ");
		listSql.append(whereParams);
		listSql.append("order by ic.cdate desc ");
		
		String sqlCount = "select count(1) from (" + listSql.toString() + ") z ";
		if (start != null && length != null) {
			listSql.append("limit " + start + ", " + length);
		}
		int count = daoLoan.getInt(sqlCount);
		
		List<InvitateFriendStat> list = daoLoan.list(listSql.toString(), InvitateFriendStat.class);
		
		if("2".equals(showway)){
			for(InvitateFriendStat stat:list){
				String invitateCountStr = stat.getInvitateCount();
				if(invitateCountStr == null){
					invitateCountStr = "0";
				}
				BigDecimal invitateCount = new BigDecimal(invitateCountStr);  //登记成功用户
				String activityCountStr = stat.getActivityCount();
				if(activityCountStr == null){
					activityCountStr = "0";
				}
				BigDecimal activityCount = new BigDecimal(activityCountStr);   //新激活用户
				String newInfoCountStr = stat.getNewInfoCount();
				if(newInfoCountStr == null){
					newInfoCountStr = "0";
				}
				BigDecimal newInfoCount = new BigDecimal(newInfoCountStr);   //创建资料成功
				String activityCountRate = activityCount.divide(invitateCount,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP).toString()+"%";
				String newInfoCountRate =  newInfoCount.divide(invitateCount,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP).toString()+"%";
				stat.setActivityCount(activityCountRate);
				stat.setNewInfoCount(newInfoCountRate);
				setUvDate(stat.getTime(), stat);  //查询点击分享人数和外部登记页uv
				stat.setActivite("邀请好友");
			}
		}else{
			for(InvitateFriendStat stat:list){
				setUvDate(stat.getTime(), stat);  //查询点击分享人数和外部登记页uv
				stat.setActivite("邀请好友");
			}
		}
		
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		return list;
	}
	
	//查询点击分享人数和外部登记页uv
	public void setUvDate(String time,InvitateFriendStat stat) throws AppException{
		String shareUrl = "/invite-friend-month/index.jsp";
		String regiUrl = "/loan-invite/index.jsp";
		String sqlUv = "SELECT p.UV uv from stat_nginx_promote p where p.url LIKE ? AND p.time = ?";
		List<InvitateFriendStat> shareStatList = dao.list(sqlUv, InvitateFriendStat.class, "%"+shareUrl+"%",time);
		List<InvitateFriendStat> regiStatList = dao.list(sqlUv, InvitateFriendStat.class, "%"+regiUrl+"%",time);
		if(shareStatList != null){
			int count = 0;
			for(InvitateFriendStat shareStat:shareStatList){
				if(shareStat.getUv() != null){
					count+=Integer.valueOf(shareStat.getUv());
				}
			}
			stat.setClickShareUv(count+"");
		}else{
			stat.setClickShareUv("0");
		}
		if(regiStatList != null){
			int count = 0;
			for(InvitateFriendStat regiStat:regiStatList){
				if(regiStat.getUv() != null){
					count+=Integer.valueOf(regiStat.getUv());
				}
			}
			stat.setExternalRegisterUv(count+"");
		}else{
			stat.setExternalRegisterUv("0");
		}
		
	}

	
}










